<?php
/** .-------------------------------------------------------------------
* |  Software: [HDCMS framework]
* |      Site: www.hdcms.com
* |-------------------------------------------------------------------
* |    Author: 向军 <2300071698@qq.com>
* |    WeChat: aihoudun
* | Copyright (c) 2012-2019, www.houdunwang.com. All Rights Reserved.
* '-------------------------------------------------------------------*/

namespace app\admin\controller;


class Category extends Common {
    private $db;
    public function __construct()
    {
        parent::__construct();
        $this->db=new \system\model\Category();
    }

    //动作
    public function index(){
    //此处书写代码...
        $data = Db::table('category')->leftjoin('type','category.tid','=','type.tid')->get();
//        p($data);die;
        //树状结构
        $treeData = Data::tree($data,'cname');
        View::with('treeData',$treeData);
//        $wData=Db::table('type')->get();
//        View::with("wData".$wData);
        return view();
    }
    public function add(){
        if(IS_POST){
//            p($_POST);die;
            if($this->db->store()){
                message("添加成功",u("index"),"success");
            }
            message($this->db->getError(),"","error");
        }
        $typeData=Db::table("type")->get();
//         p($typeData);die;
        View::with("typeData",$typeData);


        return view();
    }
    public function addSon(){
        if(IS_POST){
            if($this->db->store()){
                message("添加成功",u("index"),"success");
            }
            message($this->db->getError(),"","error");
        }
        $cid = Q('cid');
        //获得所属分类(父级分类名称)
        $cateData = Db::table('category')->where('cid',$cid)->first();
        View::with('cateData',$cateData);
//        p($cateData);DIE;
        $type=Db::table("type")->get();
        View::with("type",$type);

        return view();
    }
    //编辑
    public function edit(){
        if(IS_POST) {
            //模型返回的数据
            if($this->db->edit()){
                //成功
                message('编辑成功','','success');
            }
            //失败
            message($this->db->getError(),'','error');
        }
        //get参数
        $cid = Q('cid');
        //获得旧数据
        $oldData = Db::table('category')->where('cid',$cid)->first();
//        p($oldData);die;
        View::with('oldData',$oldData);
        //获得所属分类数据(调用模型方法)
        $cateData = $this->db->getCateData($cid);
//        p($cateData);die;
        //在option当中通过自己的pid去找寻父级的cid进行默认选中
        View::with('cateData',$cateData);
        $oldType=Db::table('type')->get();
        View::with('oldType',$oldType);
        return view();
    }

    //删除
    public function del(){
        //get参数
        $cid = Q('c');
//        p($cid);die;
        //通过父级的cid获取父级的pid
        $pid = Db::table('category')->where('cid',$cid)->pluck('pid');
        //执行替换子集pid 替换成父级的pid
        $this->db->where('pid',$cid)->update(['pid'=>$pid]);
        //执行删除父级那一条数据
        $this->db->where('cid',$cid)->delete();
        //提示跳转
        message('删除成功','','success');
    }

}
